package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.ModuleDao;
import com.itheima.domain.system.Module;
import com.itheima.service.system.ModuleService;
import com.itheima.utils.MysqlUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class ModuleServiceImpl implements ModuleService {
    // 2021/10/18 start by chenggongbing
    // 模块增删改三层
    //查询所有数据 实现分页
    @Override
    public PageInfo findByPage(String page) {
        int pageNum = page==null||page=="" ? 1 : Integer.parseInt(page);
        PageHelper.startPage(pageNum,6);
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        ModuleDao mapper = sqlSession.getMapper(ModuleDao.class);
        List<Module> all = mapper.findAll();
        PageInfo pageInfo = new PageInfo(all);
        sqlSession.close();
        return pageInfo;
    }
    //添加模块
    @Override
    public void addModule(Module module) {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        ModuleDao mapper = sqlSession.getMapper(ModuleDao.class);
        module.setModuleId(UUID.randomUUID().toString());
        mapper.addModule(module);
    }

    //删除模块
    @Override
    public void deleteModule(String id) {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        ModuleDao mapper = sqlSession.getMapper(ModuleDao.class);
        mapper.deleteModule(id);
        sqlSession.close();
    }

    //修改模块
    @Override
    public void updateModule(Module module) {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        ModuleDao mapper = sqlSession.getMapper(ModuleDao.class);
        System.out.println(module);
        //module.setModuleId(UUID.randomUUID().toString());
        mapper.updateModule(module);
        sqlSession.close();
    }

    //通过id查询
    @Override
    public Module findModuleById(String id) {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        ModuleDao mapper = sqlSession.getMapper(ModuleDao.class);
        Module moduleId = mapper.findModuleById(id);
        sqlSession.close();
        return moduleId;
    }

    public List<Module> findAll(){
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        ModuleDao mapper = sqlSession.getMapper(ModuleDao.class);
        List<Module> all = mapper.findAll();
        sqlSession.close();
        return all;
    }


}
